#include "wind_mgr.h"

#include "R.h"
#include "page_mode1.h"
#include "page_mode2.h"
#include "page_mode_add.h"
#include "PageWaterMode.h"
#include "PageSetting.h"
// #include "page_adapter.h"

void WindMgr::initWindow()
{
    mWindow = new Window(0, 0, -1, -1);
    mWindLayout = (ViewGroup *)LayoutInflater::from(mWindow->getContext())->inflate("@layout/main_window", mWindow);
    mWindPageBox = (ViewGroup *)mWindLayout->findViewById(demo::R::id::page_box);
    mWindPopBox = (ViewGroup *)mWindLayout->findViewById(demo::R::id::pop_box);

    mShowPage = nullptr;
    mPopPage = nullptr;

    mPageCloseMesg.what = MSG_PAGE_DELAY_CLOSE;
    mClosePageList.clear();
}

void WindMgr::sendMsg(int page, int type, void *data)
{
    LOGE("mShowPage = %d, page = %d", mShowPage->getDPageType(), page);
    if (mShowPage && mShowPage->getDPageType() == page)
    {
        LOGE("sendMsg");
        mShowPage->callMsg(type, data);
    }
}

void WindMgr::showPage(pageType type, bool isNewPage, int mode, int position)
{
    // LOGE("type == %d", type);
    // 当前页面与要显示的页面相同，直接返回
    if (mShowPage && mShowPage->getDPageType() == type)
        return;

#if defined(USE_DELAY_RECLCYER) && (USE_DELAY_RECLCYER)
    Looper::getMainLooper()->removeMessages(this, MSG_PAGE_DELAY_CLOSE);
    if (mShowPage)
    {
        // mShowPage->startPageAction();    // 停动画
        // if((mShowPage->getDPageType() == PAGE_WATER_MODE) && (type == PAGE_WATER_MODE_MODIFY))
        mClosePageList.push_back(mShowPage);
        mShowPage = nullptr;
    }
    Looper::getMainLooper()->sendMessageDelayed(200, this, mPageCloseMesg);

    for (auto it = mClosePageList.begin(); it != mClosePageList.end(); it++)
    {
        if ((*it)->getDPageType() == type)
        {
            (*it)->addPage();
            (*it)->startPageAction(type, position);
            mShowPage = (*it);
            mClosePageList.erase(it);
            return;
        }
    }

#else

    if (mShowPage)
    {
        mShowPage->removePage();
        delete mShowPage;
    }

#endif
    PageBase *page = nullptr;

    switch (type)
    {
    case pageType::PAGE_MODE1:
    {
        page = new PageMode1(mWindPageBox);
        break;
    }
    case pageType::PAGE_MODE2:
    {
        page = new PageMode2(mWindPageBox, mode);
        break;
    }
    case pageType::PAGE_WATER_MODE:
    {
        page = new PageWaterMode(mWindPageBox);
        break;
    }
    case pageType::PAGE_WATER_MODE_ADD:
    {
        page = new PageModeAdd(mWindPageBox, modeAction::MODE_ADD, position, PAGE_WATER_MODE_ADD);
        break;
    }
    case pageType::PAGE_WATER_MODE_MODIFY:
    {
        page = new PageModeAdd(mWindPageBox, modeAction::MODE_MODIFY, position, PAGE_WATER_MODE_MODIFY);
        break;
    }
    case pageType::PAGE_SETTING:
    {
        page = new PageSetting(mWindPageBox);
        break;
    }
    default:
        LOGE("无跳转");
        break;
    }
    mShowPage = page;
}

void WindMgr::handleMessage(Message &message)
{
    switch (message.what)
    {
    case MSG_PAGE_DELAY_CLOSE:
    {
        for (auto it = mClosePageList.begin(); it != mClosePageList.end(); it++)
        {
            // if(1){
            //     it = mClosePageList.erase(it);
            // }else{
            //     it++;
            // }
            (*it)->removePage();
            (*it)->stopPageAction();
            // delete *it;
        }
        // mClosePageList.clear();
        break;
    }
    default:
        break;
    }
}

void WindMgr::showPopPage(int type, pop_callback enterCb, pop_callback cencelCb, int position)
{
    if (mPopPage)
    {
        LOGE("pop page is exist");
        return;
    }
    mPopPage = new PagePop(mWindPopBox, type, enterCb, cencelCb, position);
    mWindPageBox->setVisibility(View::INVISIBLE);
    mWindPopBox->setVisibility(View::VISIBLE);
}

void WindMgr::dealClosePopPage()
{
    delete mPopPage;
    mPopPage = nullptr;
    mWindPopBox->setVisibility(View::INVISIBLE);
    mWindPageBox->setVisibility(View::VISIBLE);
}